﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ITM495.OOExamples.Core.Logger
{
    /// <summary>
    /// The log factory provides a common location for retriving the currently configured logger instance from.
    /// 
    /// Because the methods in this class are static it can be called from any class in your code that you want to 
    /// add logging information to. 
    /// </summary>
    /// <remarks>
    /// This is not a production ready logging implementation! It is not thread safe.
    /// 
    /// For production applications please look into using one of the following:
    /// - Microsfot System.Diagnostics namespace - http://msdn.microsoft.com/en-us/library/system.diagnostics.aspx
    /// - log4net - http://logging.apache.org/log4net/
    /// </remarks>
    public class LogManager
    {
        //static instance of the currently configured logger
        private static ILogger _currentLogger;

        /// <summary>
        /// Static method to return the currently configured logger
        /// </summary>
        /// <returns></returns>
        public static ILogger GetLogger()
        {
            return _currentLogger;
        }

        /// <summary>
        /// Initializes the LogFacory with an instance of a logger
        /// </summary>
        /// <remarks>
        /// This method should only be called once per application
        /// </remarks>
        /// <param name="logger">ILogger instance</param>
        public static void Initialize(ILogger logger)
        {
            if(_currentLogger != null)
                throw new ApplicationException("Logger has already been initialized and can not be initialized more than once.");
            
            _currentLogger = logger;
        }


        /// <summary>
        /// Creates an instance of a ILogger class by the logger name
        /// </summary>
        /// <param name="loggerName"></param>
        public static ILogger CreateLogger(string loggerName)
        {
            ILogger instance = null;

            switch(loggerName.ToLower())
            {
                case "f":
                case "file":
                    instance = new FileLogger("log.txt");
                    break;
                    
                case "c":
                case "console":
                default:
                    instance = new ConsoleLogger();
                    break;
            }

            return instance;
            
        }
    }
}